package pers.vinsonws.jianghu.clientwin.ui;

import pers.vinsonws.jianghu.clientwin.global.Info;
import pers.vinsonws.jianghu.clientwin.service.InitChatService;
import pers.vinsonws.jianghu.clientwin.service.UserService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


/**
 * @auther vinsonws
 * @date 2018/11/11 22:18
 */
public class LoginUi extends JFrame {
    private static final long serialVersionUID = 1L;
    private JPanel mainPanel;                /*主面板*/
    private JTextField userIdField;
    private JPasswordField userPwdField;
    private JButton logBut;
    private JButton RegBut;
    private ImageIcon background;                /*背景图片*/
    private JLabel backgroundCon;                /*背景图片容器*/


    public LoginUi() {
        //关闭其它界面
        addWindowListener(new WindowAdapter() {


            public void windowClosing(WindowEvent e) {

                Info.closeChannel();
            }

        });

        //设置框架
        setTitle("JH-江湖");
        setSize(1176, 664);
        //setLocation(220, 120);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        setUndecorated(false);                //隐藏标题栏

        //设置背景图片
        background = new ImageIcon("D:\\github\\online-chat-room-netty\\jianghu-client-win\\src\\main\\resources\\img\\loginui.png");
        backgroundCon = new JLabel(background);
        backgroundCon.setBounds(0, 0, background.getIconWidth(), background.getIconHeight());
        getLayeredPane().add(backgroundCon, new Integer(Integer.MIN_VALUE));
        JPanel jp = (JPanel) getContentPane();
        jp.setOpaque(false);

        //初始化mainPanel
        mainPanel = new JPanel();
        mainPanel.setOpaque(false);
        mainPanel.setLayout(null);
        setContentPane(mainPanel);


        //设置TextField
        userIdField = new JTextField();
        userPwdField = new JPasswordField();
        userIdField.setBounds(510, 424, 150, 22);
        userPwdField.setBounds(510, 474, 150, 22);
        userIdField.setForeground(Color.gray);
        userPwdField.setForeground(Color.gray);
        userIdField.setBorder(null);
        userPwdField.setBorder(null);
        userIdField.setOpaque(false);
        userPwdField.setOpaque(false);
        mainPanel.add(userIdField);
        mainPanel.add(userPwdField);

        //回车登录
        userPwdField.addKeyListener(new KeyAdapter()//键盘监听按钮
        {
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    if(userIdField.getText() != null && userPwdField.getPassword() != null){
                        if (UserService.login(userIdField.getText(), String.valueOf(userPwdField.getPassword()))){
                            //登录成功，跳转登录成功界面
                            InitChatService.initClient();
                            Info.loginUi.setVisible(false);
                            Info.indexUi.setVisible(true);
                        }else{
                            JOptionPane.showMessageDialog(null, "登陆失败","登陆异常",
                                    JOptionPane.ERROR_MESSAGE);
                        }
                    }
                }
            }
        });


        //设置按钮位置
        RegBut = new JButton("注册");
        logBut = new JButton("登录");
		RegBut.setFont(new Font("宋体",Font.PLAIN,16));
		logBut.setFont(new Font("宋体",Font.PLAIN,16));
        RegBut.setBounds(450, 540, 120, 30);
        logBut.setBounds(600, 540, 120, 30);
        RegBut.setContentAreaFilled(false);            /*异常*/
        logBut.setContentAreaFilled(false);            /*异常*/

        mainPanel.add(RegBut);
        mainPanel.add(logBut);


        RegBut.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Info.registerUi.setVisible(true);
                Info.loginUi.setVisible(false);
            }
        });


        logBut.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(userIdField.getText() != null && userPwdField.getPassword() != null){
                    if (UserService.login(userIdField.getText(), String.valueOf(userPwdField.getPassword()))){
                        //登录成功，跳转登录成功界面
                        InitChatService.initClient();
                        Info.loginUi.setVisible(false);
                        Info.indexUi.setVisible(true);
                    }else{
                        JOptionPane.showMessageDialog(null, "登陆失败","登陆异常", JOptionPane.ERROR_MESSAGE);
                    }
                }

            }
        });
        logBut.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                if(e.getKeyChar() == KeyEvent.VK_ENTER){
                    if(userIdField.getText() != null && userPwdField.getPassword() != null){
                        if (UserService.login(userIdField.getText(), String.valueOf(userPwdField.getPassword()))){
                            //登录成功，跳转登录成功界面
                            InitChatService.initClient();
                            Info.loginUi.setVisible(false);
                            Info.indexUi.setVisible(true);
                        }else{
                            JOptionPane.showMessageDialog(null, "登陆失败","登陆异常", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                }
            }
        });
    }


}
